#pragma once
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include "../vector.h"

namespace stl {
    template<typename T>
    class VectorVisualizer {
    public:
        static void visualize(const Vector<T>& vec, const std::string& name = "vector") {
            // 生成文本可视化
            std::string txt_file = "visualization/dot/" + name + ".txt";
            std::ofstream txt(txt_file);
            
            // 添加标题
            txt << "Vector Visualization: " << name << "\n";
            txt << "================================\n\n";
            
            // 添加数据节点
            txt << "Elements: [";
            for (int i = 0; i < vec.size(); i++) {
                std::stringstream ss;
                ss << vec[i];
                txt << ss.str();
                if (i < vec.size() - 1) {
                    txt << " -> ";
                }
            }
            txt << "]\n\n";
            
            // 添加容量信息
            txt << "Size: " << vec.size() << "\n";
            txt << "Capacity: " << vec.capacity() << "\n";
            
            txt.close();
            
            // 同时尝试生成图形可视化（如果安装了Graphviz）
            try {
                std::string dot_file = "visualization/dot/" + name + ".dot";
                std::ofstream dot(dot_file);
                
                // DOT文件头部
                dot << "digraph Vector {\n";
                dot << "    rankdir=LR;\n";
                dot << "    node [shape=box, style=filled, fillcolor=lightblue];\n\n";
                
                // 添加数据节点
                for (int i = 0; i < vec.size(); i++) {
                    std::stringstream ss;
                    ss << vec[i];
                    dot << "    node" << i << " [label=\"" << ss.str() << "\"];\n";
                    if (i > 0) {
                        dot << "    node" << (i-1) << " -> node" << i << ";\n";
                    }
                }
                
                // 添加容量信息
                dot << "    capacity [label=\"Capacity: " << vec.capacity() << "\", shape=note];\n";
                dot << "    size [label=\"Size: " << vec.size() << "\", shape=note];\n";
                
                dot << "}\n";
                dot.close();
                
                // 使用dot命令生成PNG文件
                std::string png_file = "visualization/png/" + name + ".png";
                std::string command = "dot -Tpng " + dot_file + " -o " + png_file;
                system(command.c_str());
            } catch (...) {
                // 如果生成图形失败，只使用文本可视化
                std::cout << "Note: Graphviz not installed. Only text visualization is available.\n";
            }
        }
    };
} 